package com.ruoyi.jetLinks.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.jetLinks.domain.DeviceType;
import com.ruoyi.jetLinks.domain.tree.DeviceTreeSelect;
import com.ruoyi.jetLinks.mapper.DeviceTypeMapper;
import com.ruoyi.jetLinks.service.IDeviceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 产品分类Service业务层处理
 *
 * @author lxx
 * @date 2022-01-27
 */
@Service
public class DeviceTypeServiceImpl implements IDeviceTypeService
{
    @Autowired
    private DeviceTypeMapper deviceTypeMapper;

    /**
     * 查询产品分类
     *
     * @param id 产品分类ID
     * @return 产品分类
     */
    @Override
    public DeviceType selectDeviceTypeById(Long id) {
        return deviceTypeMapper.selectById(id);
    }

    /**
     * 查询产品分类列表
     *
     * @param deviceType 产品分类
     * @return 产品分类
     */
    @Override
    public List<DeviceType> selectDeviceTypeList(DeviceType deviceType) {
        LambdaQueryWrapper<DeviceType> deviceTypeQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotNull(deviceType.getDeviceKey())){
            deviceTypeQueryWrapper.like(DeviceType::getDeviceKey,deviceType.getDeviceKey());
        }
        if(StringUtils.isNotNull(deviceType.getDeviceName())){
            deviceTypeQueryWrapper.like(DeviceType::getDeviceName,deviceType.getDeviceName());
        }
        deviceTypeQueryWrapper.orderByAsc(DeviceType::getParentid).orderByAsc(DeviceType::getOrderNum);
        return deviceTypeMapper.selectList(deviceTypeQueryWrapper);
    }

    /**
     * 新增产品分类
     *
     * @param deviceType 产品分类
     * @return 结果
     */
    @Override
    public int insertDeviceType(DeviceType deviceType) {
        deviceType.setDelFlag("0");
        deviceType.setDeviceStatus("0");
        return deviceTypeMapper.insert(deviceType);
    }

    /**
     * 修改产品分类
     *
     * @param deviceType 产品分类
     * @return 结果
     */
    @Override
    public int updateDeviceType(DeviceType deviceType)
    {
        return deviceTypeMapper.updateById(deviceType);
    }

    /**
     * 批量删除产品分类
     *
     * @param ids 需要删除的产品分类ID
     * @return 结果
     */
    @Override
    public int deleteDeviceTypeByIds(Long[] ids) {
        return deviceTypeMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除产品分类信息
     *
     * @param id 产品分类ID
     * @return 结果
     */
    @Override
    public int deleteDeviceTypeById(Long id) {
        return deviceTypeMapper.deleteById(id);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param menus 菜单列表
     * @return 下拉树结构列表
     */
    @Override
    public List<DeviceTreeSelect> buildDeviceTypeMenuTree(List<DeviceType> menus) {
        List<DeviceType> menuTrees = buildMenuTree(menus);
        return menuTrees.stream().map(DeviceTreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param menus 菜单列表
     * @return 树结构列表
     */
    @Override
    public List<DeviceType> buildMenuTree(List<DeviceType> menus) {
        List<DeviceType> returnList = new ArrayList<>();
        List<Long> tempList = new ArrayList<Long>();
        for (DeviceType dept : menus){
            tempList.add(dept.getId());
        }
        for (Iterator<DeviceType> iterator = menus.iterator(); iterator.hasNext();){
            DeviceType menu = iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentid())){
                recursionFn(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty()){
            returnList = menus;
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<DeviceType> list, DeviceType t){
        // 得到子节点列表
        List<DeviceType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (DeviceType tChild : childList) {
            if (hasChild(list, tChild)){
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<DeviceType> getChildList(List<DeviceType> list, DeviceType t){
        List<DeviceType> tlist = new ArrayList<>();
        Iterator<DeviceType> it = list.iterator();
        while (it.hasNext()){
            DeviceType n =  it.next();
            if (n.getParentid().longValue() == t.getId().longValue()){
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<DeviceType> list, DeviceType t){
        return getChildList(list, t).size() > 0 ? true : false;
    }
}